<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js" type="application/javascript"></script>
</head>
<body>
  <div id="app">
    <my-component @set-username="setUsername"></my-component>
    <div>{{localName}}</div>
  </div>
</body>
<script>
  // 通过子传父,$emit发消息
  Vue.component('my-component', {
    data() {
      return {
        username: 'zs'
      }
    },
    template: `
      <div>子组件</div>
    `,
    created() {
      this.$emit('set-username', this.username)
    },
  })
  let vm = new Vue({
    el: '#app',
    data() {
      return {
        localName: ''
      }
    },
    methods: {
      setUsername(data){
        this.localName = data
      }
    },
  })
</script>
</html>
